package cn.edu.scau.sec.rw.controllers;

import cn.edu.scau.sec.rw.model.disk_file.file.ExeFile;
import cn.edu.scau.sec.rw.model.disk_file.file.TextFile;
import cn.edu.scau.sec.rw.model.disk_file.file.VirtualFile;
import cn.edu.scau.sec.rw.model.disk_file.utils.File_DiskUtil;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class CreateFileController implements Initializable {

    @FXML
    private Button cancel;

    @FXML
    private AnchorPane Apane;

    @FXML
    private Button submit;

    @FXML
    private TextField textField;

    @FXML
    private TextArea textArea;

//    public CreateFileController(){}
//
//    public CreateFileController(TreeItem<VirtualFile> catalog , Stage stage){
//        this.catalog = catalog;
//        this.stage = stage;
//    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        submit.setOnAction(event -> {

            String type = textField.getText();
            if (type.contains(".e")){
                String filename = textField.getText();
                String content = textArea.getText();
                System.out.println("内容：" + content);
                String[] statement = content.split("\\r?\\n");
                for (String s : statement) {
                    System.out.println(s);
                }
                ArrayList<String> statements = new ArrayList<>();
                for (String s : statement) {
                    statements.add(s);
                }
                ExeFile exeFile = new ExeFile(filename, statements, statements.size());
                TreeItem<VirtualFile> item = new TreeItem<>(exeFile);
                CommonBridge.item.getChildren().add(item);
            }
            else {
                String filename = textField.getText();
                String content = textArea.getText();
                TextFile textFile = new TextFile(filename, content, content.length()/3);
                TreeItem<VirtualFile> item = new TreeItem<>(textFile);
                CommonBridge.item.getChildren().add(item);
                File_DiskUtil.saveVirtualFile(textFile);
            }


        });

        cancel.setOnAction(event -> {

            CommonBridge.stage.close();
        });
    }
}
